package com.gec.system.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gec.model.entity.SysRole;

import com.gec.model.vo.AssginRoleVo;
import com.gec.model.vo.SysRoleQueryVo;
import com.gec.system.service.SysRoleService;
import com.gec.system.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author Liangxj
 * @since 2023-12-06
 */

@Api(tags = "角色管理控制器")
@RestController

@RequestMapping("/admin/system/sysRole")
public class SysRoleController {

    @Autowired
    private SysRoleService SysRoleService;

//    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @ApiOperation("获取角色列表")
    @GetMapping("/roleList")
    public Result selectAll(){

//            int i = 1/0;

        List<SysRole> list = this.SysRoleService.list();
        return Result.ok(list);
    }

//    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @ApiOperation("分页查询")
    @GetMapping("/{page}/{limit}")
    public Result selectPage(@PathVariable Long page,
                             @PathVariable Long limit,
                             SysRoleQueryVo sysRoleQueryVo){
        IPage<SysRole> page1 = new Page<>(page,limit);
        page1 = this.SysRoleService.selectPage(page1, sysRoleQueryVo);
        return Result.ok(page1);

    }

//    @PreAuthorize("hasAuthority('bnt.sysRole.remove')")
    @ApiOperation("逻辑删除角色")
    @DeleteMapping("/removeRole/{id}")
    public Result removeRole(@PathVariable Long id){
        boolean b = this.SysRoleService.removeById(id);
        if (b) {
            return Result.ok();
        }
        return Result.fail();
    }


//    @PreAuthorize("hasAuthority('bnt.sysRole.add')")
    @ApiOperation("添加一个角色")
    @PostMapping("/addRole")
    public Result addRole(@RequestBody SysRole sysRole){

        boolean save = this.SysRoleService.save(sysRole);
        if (save){
            return Result.ok();
        }
        return Result.fail();
    }

//    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @ApiOperation("根据id查找角色")
    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable Long id){
        SysRole sysRole = this.SysRoleService.getById(id);
        return Result.ok(sysRole);
    }

//    @PreAuthorize("hasAuthority('bnt.sysRole.update')")
    @ApiOperation("根据id修改角色")
    @PostMapping("/updateRole")
    public Result updateById(@RequestBody SysRole sysRole){

        boolean b = this.SysRoleService.updateById(sysRole);
        if (b){
            return Result.ok();
        }
        return Result.fail();

    }


//    @PreAuthorize("hasAuthority('bnt.sysRole.remove')")
    @ApiOperation("批量删除ids角色")
    @DeleteMapping("/removeByIds")
    public Result removeByIds(@RequestBody List<Long> ids){


        boolean b = this.SysRoleService.removeByIds(ids);
        if (b){
            return Result.ok();
        }
        return Result.fail();
    }

    @ApiOperation("通过countid获取用户角色")
    @GetMapping("/getRolesByCountId/{id}")
    public Result getRolesByCountId(@PathVariable Long id){
        Map<String,Object> map = this.SysRoleService.getRolesByCountId(id);
        return Result.ok(map);
    }


    @ApiOperation("给用户分配角色通过id")
    @PostMapping("/doAssign")
    public Result doAssign(@RequestBody AssginRoleVo assginRoleVo){

        this.SysRoleService.doAssign(assginRoleVo);
        return Result.ok();
    }




}
